//
//  AttachPhotoViewController.m
//  Flava
//
//  Created by Ug-Code on 13-9-2.
//  Copyright (c) 2013年 UgCode. All rights reserved.
//

#import "AttachPhotoViewController.h"

@implementation PhotoDummyView

@synthesize
numberLable = _numberLable;

- (id)init
{
    self = [super init];
    if (self) {
		self.frame = CGRectMake(0, 0, 85, 85);
        self.backgroundImage = [UIImage imageNamed:@"photo_dummy.png"];
		_numberLable = [[UILabel alloc] init];
		_numberLable.frame = CGRectMake(0, 0, 35, 35);
		_numberLable.center = CGPointMake(85/2, 85/2);
		_numberLable.textColor = [UIColor colorWithRed:200.0f/255 green:200.0f/255 blue:200.0f/255 alpha:1];
		_numberLable.font = [UIFont boldSystemFontOfSize:35];
		_numberLable.textAlignment = NSTextAlignmentCenter;
		[self addSubview:_numberLable];
    }
    return self;
}

@end

@interface AttachPhotoViewController ()

@end

@implementation AttachPhotoViewController

@synthesize
photoDummy1 = _photoDummy1,
photoDummy2 = _photoDummy2,
photoDummy3 = _photoDummy3,
photoDummy4 = _photoDummy4,
photoDummy5 = _photoDummy5;

- (void)viewDidLoad
{
    [super viewDidLoad];
	self.view.backgroundColor = [UIColor redColor];
	[self createBgView];
	[self createTitleIcon];
	[self createCompleteButton];
	[self createImagePickerButton];
	[self createPhotoDummy];
}

- (void)completeButtonClick:(UIButton *)button
{
	[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerCamera:(UIButton *)button
{
	if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
		UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
		pickerController.delegate = self;
		pickerController.allowsEditing = YES;
		pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
		[self presentViewController:pickerController animated:YES completion:nil];
	} else {
		NSLog(@"NoCamera!");
	}
}

- (void)imagePickerLibrary:(UIButton *)button
{
	UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
	pickerController.delegate = self;
	pickerController.allowsEditing = YES;
	pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
	[self presentViewController:pickerController animated:YES completion:nil];
}

- (void)createBgView
{
	_titleBgView = [[UGView alloc] init];
	_titleBgView.frame = CGRectMake(0, 0, 320, 164);
	_titleBgView.backgroundImage = [[UIImage imageNamed:@"title_bg.png"]
									stretchableImageWithLeftCapWidth:3 topCapHeight:5];
	[self.view addSubview:_titleBgView];
	
	_photosBgView = [[UGView alloc] init];
	_photosBgView.frame = CGRectMake(0, 164, 320, [UGDevice windowHeight]-20-164);
	_photosBgView.backgroundImage = [[UIImage imageNamed:@"text_view_bg.png"]
									 stretchableImageWithLeftCapWidth:1 topCapHeight:1];
	[self.view addSubview:_photosBgView];
}

- (void)createTitleIcon
{
	_titleIconImage = [[UGImageView alloc] init];
	_titleIconImage.frame = CGRectMake(130, 6, 60, 33);
	_titleIconImage.image = [UIImage imageNamed:@"title_icon_photo.png"];
	[_titleBgView addSubview:_titleIconImage];
}

- (void)createCompleteButton
{
	_completeButton = [[UGButton alloc] init];
	_completeButton.frame = CGRectMake(260, 6, 55, 33);
	[_completeButton setNormalImage:[[UIImage imageNamed:@"btn_blue_n.png"]
									 stretchableImageWithLeftCapWidth:1 topCapHeight:1]];
	[_completeButton setHighlightedImage:[[UIImage imageNamed:@"btn_blue_h.png"]
									 stretchableImageWithLeftCapWidth:1 topCapHeight:1]];
	[_completeButton setNormalTitle:@"完成"];
	_completeButton.titleLabel.font = [UIFont boldSystemFontOfSize:13];
	[_titleBgView addSubview:_completeButton];
	
	[_completeButton addTarget:self
						action:@selector(completeButtonClick:)
			  forControlEvents:UIControlEventTouchUpInside];
}

- (void)createImagePickerButton
{
	_imagePickerCameraButton = [[UGButton alloc] init];
	_imagePickerCameraButton.frame = CGRectMake(15, 54, 290, 43);
	[_imagePickerCameraButton setNormalImage:[[UIImage imageNamed:@"btn_common_n.png"]
											  stretchableImageWithLeftCapWidth:5 topCapHeight:5]];
	[_imagePickerCameraButton setHighlightedImage:[[UIImage imageNamed:@"btn_common_h.png"]
											  stretchableImageWithLeftCapWidth:5 topCapHeight:5]];
	[_imagePickerCameraButton setNormalTitle:@"照相"];
	_imagePickerCameraButton.titleLabel.font = [UIFont boldSystemFontOfSize:17];
	[_imagePickerCameraButton setNormalTitleColor:
	 [UIColor colorWithRed:110.0f/255 green:110.0f/255 blue:110.0f/255 alpha:1]];
	[_titleBgView addSubview:_imagePickerCameraButton];
	
	[_imagePickerCameraButton addTarget:self
								 action:@selector(imagePickerCamera:)
					   forControlEvents:UIControlEventTouchUpInside];
	
	_imagePickerLibraryButton = [[UGButton alloc] init];
	_imagePickerLibraryButton.frame = CGRectMake(15, 107, 290, 43);
	[_imagePickerLibraryButton setNormalImage:[[UIImage imageNamed:@"btn_common_n.png"]
											  stretchableImageWithLeftCapWidth:5 topCapHeight:5]];
	[_imagePickerLibraryButton setHighlightedImage:[[UIImage imageNamed:@"btn_common_h.png"]
												   stretchableImageWithLeftCapWidth:5 topCapHeight:5]];
	[_imagePickerLibraryButton setNormalTitle:@"从相册选择"];
	_imagePickerLibraryButton.titleLabel.font = [UIFont boldSystemFontOfSize:17];
	[_imagePickerLibraryButton setNormalTitleColor:
	 [UIColor colorWithRed:110.0f/255 green:110.0f/255 blue:110.0f/255 alpha:1]];
	[_titleBgView addSubview:_imagePickerLibraryButton];
	
	[_imagePickerLibraryButton addTarget:self
								  action:@selector(imagePickerLibrary:)
						forControlEvents:UIControlEventTouchUpInside];
}

- (void)createPhotoDummy
{
	for (int i = 0; i < 5; i++) {
		@autoreleasepool {
			PhotoDummyView *photoDummy = [[PhotoDummyView alloc] init];
			photoDummy.frame = CGRectMake(13+((i%3)*102),17+((i/3)*102), 85, 85);
			photoDummy.numberLable.text = [NSString stringWithFormat:@"%d", i+1];
			[self setValue:photoDummy forKey:[NSString stringWithFormat:@"photoDummy%d", i+1]];
			[_photosBgView addSubview:photoDummy];
		}
	}
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
